package com.xq.model.service.impl;

import com.alibaba.fastjson.JSON;
import com.xq.config.properties.ManageProperties;
import com.xq.model.dto.PostDataTransferDTO;
import com.xq.model.mapper.TBaseStationStateMapper;
import com.xq.model.mapper.TBaseSurfMinMapper;
import com.xq.model.service.DataTransferService;
import com.xq.utils.HttpUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 数据传输统计 实现层
 *
 * @author xu
 * 2021/9/15 11:39
 */
@Service
public class DataTransferServiceImpl implements DataTransferService {
    @Resource
    private ManageProperties manageProperties;
    @Resource
    private TBaseSurfMinMapper surfMinMapper;
    @Resource
    private TBaseStationStateMapper stationStateMapper;

    @Override
    public void postDataTransfer(String tranType) throws IOException {
        List<PostDataTransferDTO> dtoList = new ArrayList<>();
        // 地面要素
        if("1".equals(tranType)){
             dtoList = surfMinMapper.countDataTransfer();
        }
        // 站点状态
        if("2".equals(tranType)){
            dtoList = stationStateMapper.countDataTransfer();
        }
        // 真正取到值了再说
        if(dtoList.size() != 0){
            // 记得设置类型 避免出问题
            dtoList.forEach(x->{
                x.setTranType(tranType);
                // 实报的数量等于 及时的加超时的
                x.setRealNum(x.getInNum()+ x.getOverNum());
            });
            String jsonStr = JSON.toJSONString(dtoList);
            HttpUtils.sendPostJson(manageProperties.getHost()+manageProperties.getDataTransferPath(),jsonStr);
        }
    }
}
